<?php

namespace Api\Controller;

class LoginController extends ApiController{
    
    public function login(){
        echo 1;die;
        $data = I('post.','','addslashes');
        if(empty($data)){
            $this->error('请输入用户名密码');
        }
        
        if(!isset($data['username']) || empty($data['username'])){
            $this->error('用户名和密码不能为空');
        }

        if(!isset($data['password']) || empty($data['password'])){
            $this->error('用户名和密码不能为空');
        }
        $where = array();
        if(isMobile($data['username'])){
            $where['mobile'] = $data['username'];
            $where['nickname'] = $data['username'];
            $where['_logic'] = 'OR';
        }elseif(isEmail($data['username'])){
            $where['email'] = $data['username'];
        }else{
            $where['nickname'] = $data['username'];
        }

        $member = M('User')->where($where)->find();

        if(empty($member)){
            $this->error('用户不存在');
        }

        if($member['status'] == 0){
            $this->error('该账号还没有激活，请先激活');
        }

        if($member['password'] != entrypt_password($data['password'])){
            $this->error('用户名或密码错误');
        }

        if($member['available'] == 0){
            $this->error('该账号暂时不能登录');
        }

        $token_arr = array(
            'id'        =>  $member['id'],
            'nickname'  =>    $member['nickname'],
            'reg_time'  =>  $member['reg_time'],
            'time'      =>  time(),
        ); 

        ksort($token_arr);
        $token_str = http_build_query($token_arr,'','&amp');
        $token = md5($token_str);
        
        //更新登录时间
        M('User')->where(['id' => $member['id']])->setField(array('login_time'=> time(),'token'=>$token));
        
        $this->success($this->loginAuth($member['id']),'登录成功');
    }
    
    
    //用户注册
    public function register(){
        $mobile = I('post.mobile');
        $email = I('post.email');
        $password = I('password');
        $passwordr = I('passwordr');

        $reg_type = I('reg_type');
        $mobile_code = I('mobile_code');
        $nickname = I('nickname','','addslashes');
        if(!isset($_POST['agreement'])){
            $this->error('请先阅读用户协议');
        }

        if(empty($nickname)){
            $this->error('请为自己设置一个昵称');
        }else{
            $has_name = M('User')->where(array('nickname'=>$nickname))->count();
            if($has_name > 0){
                $this->error($mobile.'该昵称已使用');
            }
        }

        $data = array(); 
        if($reg_type == 'mobile'){
            if(!isMobile($mobile) || !S('get_sms_'.$mobile)){
                $this->error('手机号错误');
            }
            
            if($mobile_code != S('sms_'.$mobile)){
                $this->error('手机验证码错误');
            }
            
            $has_name = M('User')->where(array('mobile'=>$mobile))->count();
            if($has_name > 0){
                $this->error($mobile.'手机号已注册');
            }
            $data['mobile'] = $mobile;
            $data['mobile_bind'] = 1;
            $data['status'] = 1;
        }elseif($reg_type == 'email'){
            if(!isEmail($email)){
                $this->error('邮箱格式错误');
            }

            $has_name = M('User')->where(array('email'=>$email))->count();
            if($has_name > 0){
                $this->error($email.'邮箱已注册');
            }
            $data['email'] = $email;
        }else{
            $this->error('暂不支持这种注册方式');
        }

        if(empty($password)){
            $this->error('密码不能为空');
        }elseif (strlen($password) < 6){
            $this->error('密码至少6位');
        }

        if($password != $passwordr){
            $this->error('两次密码输入不一致');
        }

        $data['nickname'] = $nickname;
        $data['password'] = entrypt_password($password);
        $data['reg_time'] = time();
        $data['reg_platform'] = 'PC';
        $data['avatar'] = '/Public/avatar/default.jpg';
        if(M('User')->data($data)->add() !== false){
            $msg = "注册成功";
            if($reg_type == 'mobile'){
                S('get_sms_'.$mobile, null);
                S('sms_'.$mobile, null);
            }elseif($reg_type == 'email'){
                $msg = "邮件已发送至邮箱{$email}，请登录邮箱激活"; 
                $last_id = M('User')->getLastInsID();
                $md5_str = md5($email.'_'.$last_id);
                $content = "用户 {$email} 你好，你在{$this->setting['site_name']}注册了账号，请点击已下链接激活账户，如果不能点击，请复制链接到浏览器的地址栏。{$this->setting['site_url']}/Wap/Active/act/dhq/{$md5_str}-{$last_id}";
                $this->send_mail($email, $content);
            }
            $this->success(array('url'=>U('Login/login'),'time'=>1000),$msg);
        }
        $this->error('注册失败'.M('User')->getError());
    }
    
    //忘记密码-重新设置密码
    public function forget(){
        $forget_type = I('forget_type');
        $user = array();

        if($forget_type == 'mobile'){
            $mobile = I('mobile');  //手机号
            $mobile_sms = I('mobile_sms',0,'intval');  //短信验证码

            if(!isMobile($mobile)){
                $this->error('手机格式错误');
            }

            if(!S('get_sms_'.$mobile)){
                $this->error('请使用接收验证码的手机号');
            }

            if($mobile_sms != S('sms_'.$mobile)){
                $this->error('手机验证码错误');
            }

            $user = M('User')->where(array('mobile'=>$mobile))->find();

        }elseif($forget_type == 'email'){
            $email = I('email');
            if(!isEmail($email)){
                $this->error('邮箱格式错误');
            }

            $email_code = I('email_code',0,'intval');
            if($email_code != session('forget_email')){
                $this->error('邮箱验证码错误');
            }

            $user = M('User')->where(array('email'=>$email))->find();
        }

        if(empty($user)){
             $this->error('该用户不存在');
        }else{
            $password = I('password');
            $passwordr = I('passwordr');
            if(empty($password)){
                $this->error('密码不能为空');
            }
            if(strlen($password) < 6){
                $this->error('密码最少6位');
            }
            if($password != $passwordr){
                $this->error('两次密码输入不一致');
            }

            $new_password = entrypt_password($password);
            $res = M('User')->where(array('id'=>$user['id']))->setField('password', $new_password);
            if($res !== false){
                $this->success(array(),'密码修改成功');
            }
            
            $this->error('密码修改失败');
        }
    }
    
    //发送邮件
    public function send_email(){
        $email = I('email');
        if(!isEmail($email)){
            $this->error('邮箱格式错误');
        }
        $user = M('User')->where(array('email'=>$email))->count();
        if(empty($user)){
            $this->error('该用户不存在');
        }
        
        $code = mt_rand(111111, 999999);
        S('forget_email_'.$email, $code, 600);
        $content = '用户找回密码，验证码'.$code.'，有效期10分钟';
        
        if($this->send_mail($email, $content)){
            $this->success('邮件已发送');
        }
        
        $this->error('邮件发送失败');
    }
    
}

